function subdomainVisits(cpdomains: string[]): string[] {
	const domainMap: Map<string, number> = new Map();
	const ans: string[] = [];

	for (let domainInfo of cpdomains) {
		let [initVal, domain] = domainInfo.split(' ');

		while (domain) {
			const value = domainMap.get(domain) || 0;
			domainMap.set(domain, value + +initVal);

			const dotIndex = domain.indexOf('.');
			if (dotIndex < 0) {
				break;
			}

			domain = domain.slice(dotIndex + 1);
		}
	}

	for (const [domain, times] of domainMap.entries()) {
		ans.push([times, domain].join(' '));
	}

	return ans;
}
